<template>
  <el-dialog :title="!dataForm.roleId ? '新增' : '修改'" :visible.sync="visible" width="60%">
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
    
      label-width="110px"
    >
      <el-form-item label="宠物名称" prop="name">
        <el-input v-model="dataForm.name" clearable placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="年龄" prop="age">
        <el-input v-model="dataForm.age" clearable type="number" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="分类" prop="pet_name">
        <el-select v-model="dataForm.petName" clearable placeholder="请选择" style="width: 100%;">
          <el-option label="猫" value="猫"></el-option>
          <el-option label="狗" value="狗"></el-option>
          <el-option label="其他" value="其他"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="性别" prop="gender">
        <el-select v-model="dataForm.gender" clearable placeholder="请选择" style="width: 100%;">
          <el-option label="男" value="男"></el-option>
          <el-option label="女" value="女"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="宠物性格" prop="pet_character">
        <el-input v-model="dataForm.petCharacter" clearable type="text" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="宠物血型" prop="blood_group">
        <el-input v-model="dataForm.bloodGroup" clearable type="text" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="品种" prop="variety">
        <el-input v-model="dataForm.variety" clearable type="text" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="肩高" prop="shoulder_height">
        <el-input v-model="dataForm.shoulderHeight" clearable type="text" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="体重" prop="weight">
        <el-input v-model="dataForm.weight" clearable type="text" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="生日" prop="birthday">
        <el-date-picker
          v-model="dataForm.birthday"
          type="date"
          value-format="yyyy-MM-dd"
          clearable
          placeholder="选择日期"
          style="width: 100%"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="是否绝育" prop="sterilization">
        <el-select
          v-model="dataForm.sterilization"
          clearable
          placeholder="请选择"
          style="width: 100%;"
        >
          <el-option label="是" value="是"></el-option>
          <el-option label="否" value="否"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="免疫情况" prop="immune">
        <el-input v-model="dataForm.immune" clearable type="text" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="过往病史" prop="medical_history">
        <el-input type="textarea" v-model="dataForm.medicalHistory" clearable placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="图片" prop="picture">
        <el-upload
          class="upload-demo"
          action
          accept="image/*"
          :http-request="handleUpload"
          :before-upload="beforeUpload"
          list-type="picture-card"
          :on-remove="handleRemove"
          :fileList="fileList"
        >
          <i class="el-icon-plus"></i>
        </el-upload>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getDataInfo, savaDataInfo ,upload } from "@/api/pet";
import { mapGetters } from "vuex";
export default {
  data() {
    return {
      visible: false,
      dataForm: {},
      dataRule: {
        name: { required: true, message: "请输入", trigger: "blur" },
        age: { required: true, message: "请输入", trigger: "blur" },
        gender: { required: true, message: "请输入", trigger: "blur" }
      },
      fileList: []
    };
  },
  computed: {
    ...mapGetters(["userInfo"])
  },
  mounted() {},
  methods: {
    beforeUpload(file) {
      if (!file.type.startsWith("image")) {
        this.$message.warning("请上传图片");
        return false;
      }
    },
    // 上传图片
    handleUpload(file) {
      let formData = new FormData();
      formData.append("file", file.file);
      upload(formData).then(({ fileInfo }) => {
        fileInfo.filePath ? this.fileList.push({ url: fileInfo.filePath }) : '';
        this.dataForm.picture = fileInfo.filePath || ''
      });
    },
    // 删除图片
    handleRemove(file) {
      this.fileList = this.fileList.filter(function(item) {
        return item.url !== file.url;
      });
    },
    init(id) {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        this.dataForm = {
          id,
          userId: this.userInfo.id,
          nickName: this.userInfo.name
        };
        if (id) {
          getDataInfo(id)
            .then(data => {
              if (data && data.code === 0) {
                this.dataForm = data.data;
                this.fileList = data.data.picture ? [
                  {
                    url: data.data.picture || ""
                  }
                ] : [];
              }
            })
            .catch(err => {
              console.log(err);
            });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          savaDataInfo(this.dataForm)
            .then(res => {
              if (res && res.code === 0) {
                this.$message({
                  message: "操作成功",
                  type: "success",
                  duration: 1500,
                  onClose: () => {
                    this.visible = false;
                    this.$emit("refreshDataList");
                  }
                });
              }
            })
            .catch(err => {
              console.log(err);
            });
        }
      });
    }
  }
};
</script>
